﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RushStarRewardPage : Page
    {
        public UILabel currentStarNumLabel;
        public UILabel currentRewardDetailLabel;

        public GameObject itemPrefab;
        public UIGrid grid;

        public List<UITweener> tweeners;

        GameObject threeItem;
        GameObject sixItem;
        GameObject nineItem;
        protected override void DoOpen()
        {
            ProtoBuf.Message.CSRushRewardPreview msg = new ProtoBuf.Message.CSRushRewardPreview();
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRushRewardPreview, msg, (pack, b) => {
                if (b) {
                    ProtoBuf.Message.SCRushRewardPreview _msg = pack.kBody as ProtoBuf.Message.SCRushRewardPreview;
                    Init(_msg);
                }
            });

            float tweenerTime = 0f;
            foreach (var tweener in tweeners) {
                if (tweener != null) {
                    tweener.ResetToBeginning();
                    tweener.PlayForward();
                    if (tweenerTime <= tweener.duration + tweener.delay) {
                        tweenerTime = tweener.duration + tweener.delay;
                    }
                }
            }
            Invoke("OnTweenerOver", tweenerTime);

        }

        void OnTweenerOver() {
            threeItem.SetActive(true);
            sixItem.SetActive(true);
            nineItem.SetActive(true);
            grid.Reposition();
        }

        protected override void DoClose()
        {
        }

        public void Clear() {
            GameObject.Destroy(threeItem);
            GameObject.Destroy(sixItem);
            GameObject.Destroy(nineItem);
        }

        public void Init(ProtoBuf.Message.SCRushRewardPreview msg) {
            currentStarNumLabel.text = msg.sectionStar.ToString();
            currentRewardDetailLabel.text = string.Format(TableTextBlo.GetText("get_rush_reward_detail"), msg.sectionFrom, msg.sectionTo);
            Clear();
            threeItem = GameObject.Instantiate(itemPrefab) as GameObject;
            threeItem.transform.SetParent(grid.transform);
            threeItem.transform.localPosition = Vector3.zero;
            threeItem.transform.localEulerAngles = Vector3.zero;
            threeItem.transform.localScale = Vector3.one;
            threeItem.GetComponent<RushStarRewardItem>().Init(msg.threeStarReward, RushStarRewardItemType._3);
            threeItem.SetActive(false);

            sixItem = GameObject.Instantiate(itemPrefab) as GameObject;
            sixItem.transform.SetParent(grid.transform);
            sixItem.transform.localPosition = Vector3.zero;
            sixItem.transform.localEulerAngles = Vector3.zero;
            sixItem.transform.localScale = Vector3.one;
            sixItem.GetComponent<RushStarRewardItem>().Init(msg.sixStarReward, RushStarRewardItemType._6);
            sixItem.SetActive(false);

            nineItem = GameObject.Instantiate(itemPrefab) as GameObject;
            nineItem.transform.SetParent(grid.transform);
            nineItem.transform.localPosition = Vector3.zero;
            nineItem.transform.localEulerAngles = Vector3.zero;
            nineItem.transform.localScale = Vector3.one;
            nineItem.GetComponent<RushStarRewardItem>().Init(msg.nineStarReward, RushStarRewardItemType._9);
            nineItem.SetActive(false);
        }

    }

}